﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using System.Configuration;
using System.IO.Ports;
using System.Threading;

namespace ChangWuDiBang
{
    public partial class FrmMain : Office2007Form
    {
        //private delegate void UpdateTextEventHandler(string text);  //委托，此为重点
        //private UpdateTextEventHandler updateText;
        //private void UpdateTextBox(string text)
        //{
        //    lblVSerialPortRead.Text = text;
        //}

        public UserInfo _UserInfo
        {
            get;
            set;
        }

        public FrmMain()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.lblVWelCome.Text = "欢迎您,"+_UserInfo.UserName;
            Bar2ButtonShow(Bar2Mode.Init);

            //updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象

            SerialPortInit();//初始化串口
        }

        private enum Bar2Mode
        { 
            Init,System,DiBang,Help
        }

        private void Bar2ButtonShow(Bar2Mode mode)
        {
            //UserGroup分为三个等级:Administrator,User,Viewer,对应管理员,使用者
            switch (mode)
            { 
                case Bar2Mode.Init:
                    if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
                    {
                        btnFirstDiBang.Visible = true;
                        btnSecDiBang.Visible = true;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = true;
                        btnDiBangHistory.Visible = true;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_User))
                    {
                        btnFirstDiBang.Visible = true;
                        btnSecDiBang.Visible = true;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = true;
                        btnDiBangHistory.Visible = true;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Viewer))
                    {
                        btnFirstDiBang.Visible = false;
                        btnSecDiBang.Visible = false;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = false;
                        btnDiBangHistory.Visible = true;
                    }
                    btnPwdChange.Visible = false;
                    btnUserManager.Visible = false;
                    btnSystemEdit.Visible = false;
                    break;
                case Bar2Mode.System:
                    if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
                    {
                        btnPwdChange.Visible = true;
                        btnUserManager.Visible = true;
                        btnSystemEdit.Visible = true;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_User))
                    {
                        btnPwdChange.Visible = true;
                        btnUserManager.Visible = false;
                        btnSystemEdit.Visible = false;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Viewer))
                    {
                        btnPwdChange.Visible = true;
                        btnUserManager.Visible = false;
                        btnSystemEdit.Visible = false;
                    }
                    btnFirstDiBang.Visible = false;
                    btnSecDiBang.Visible = false;
                    btnDiBangInput.Visible = false;
                    btnDiBangEdit.Visible = false;
                    btnDiBangHistory.Visible = false;
                    break;
                case Bar2Mode.DiBang:
                    if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
                    {
                        btnFirstDiBang.Visible = true;
                        btnSecDiBang.Visible = true;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = true;
                        btnDiBangHistory.Visible = true;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_User))
                    {
                        btnFirstDiBang.Visible = true;
                        btnSecDiBang.Visible = true;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = true;
                        btnDiBangHistory.Visible = true;
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Viewer))
                    {
                        btnFirstDiBang.Visible = false;
                        btnSecDiBang.Visible = false;
                        btnDiBangInput.Visible = false;
                        btnDiBangEdit.Visible = false;
                        btnDiBangHistory.Visible = true;
                    }
                    btnPwdChange.Visible = false;
                    btnUserManager.Visible = false;
                    btnSystemEdit.Visible = false;
                    break;
                case Bar2Mode.Help:
                    if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Administrator))
                    {
                        
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_User))
                    {
                        
                    }
                    else if (_UserInfo.UserGroup.Equals(ConstParam.UserGroup_Viewer))
                    {
                        
                    }
                    break;
                default:
                    break;
                    
            }

        }

        private void btnSystem_Click(object sender, EventArgs e)
        {
            Bar2ButtonShow(Bar2Mode.System);
        }

        private void btnDiBang_Click(object sender, EventArgs e)
        {
            Bar2ButtonShow(Bar2Mode.DiBang);
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Bar2ButtonShow(Bar2Mode.Help);

            //FrmHelp frmhelp = new FrmHelp();
            //frmhelp.ShowDialog();
        }

        private void btnPwdChange_Click(object sender, EventArgs e)
        {
            FrmPwdChange frm = new FrmPwdChange();
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnUserManager_Click(object sender, EventArgs e)
        {
            FrmUserManager frm = new FrmUserManager();
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBoxEx.Show("确定要退出吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                //this.port.Dispose();
                this.port.Close();
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void btnDiBangInput_Click(object sender, EventArgs e)
        {
            FrmDiBangInput frm = new FrmDiBangInput();
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnFirstDiBang_Click(object sender, EventArgs e)
        {
            FrmDiBangFirstInput frm = new FrmDiBangFirstInput();
            frm._SerialPort = this.port;
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnSecDiBang_Click(object sender, EventArgs e)
        {
            FrmDiBangSecQuery frm = new FrmDiBangSecQuery();
            frm._SerialPort = this.port;
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnDiBangEdit_Click(object sender, EventArgs e)
        {
            FrmDiBangQuery frm = new FrmDiBangQuery();
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnDiBangHistory_Click(object sender, EventArgs e)
        {
            FrmDiBangHistory frm = new FrmDiBangHistory();
            frm._UserInfo = this._UserInfo;
            frm.ShowDialog();
        }

        private void btnSystemEdit_Click(object sender, EventArgs e)
        {
            //FrmBase64Encoding frm = new FrmBase64Encoding();
            //frm.ShowDialog();
        }

        //接收SerialPort数据
        private void SerialPortInit()
        {
            string portName = ConfigurationManager.AppSettings["PortName"].ToString();
            string baudRate = ConfigurationManager.AppSettings["BaudRate"].ToString();//数值需要与地磅设定相同

            int iBaudRate = int.Parse(baudRate);

            port.PortName = portName;
            port.BaudRate = iBaudRate; 
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;

            try
            {
                //port.Close();
                //port.Dispose();

                port.Open();

                //this.port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.port_DataReceived);
                
                lblVSerialPortState.Text = "端口OK!";
                //lblVSerialPortState.BackColor = Color.LightGreen;
            }
            catch (Exception err)
            {
                MessageBoxEx.Show(err.Message, "Warming", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lblVSerialPortState.Text = "端口异常!";
                lblVSerialPortState.BackColor = Color.Red;
            }
        }

        //private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        //{
        //    Thread.Sleep(500);
        //    Thread thread = new Thread(() =>
        //    {

        //        byte[] readBuffer = new byte[port.BytesToRead];
        //        port.Read(readBuffer, 0, readBuffer.Length);
        //        string result = SerialPortProcess.GetBuffer(readBuffer).ToString();

        //        ConstParam.DiBangRead = result;

        //        //
        //        //this.Invoke(updateText, new string[] { ConstParam.DiBangRead });
        //    });

        //    thread.Start();
        //}
    }
}
